import * as fs from 'fs';

import * as file_ from '../util/file_';

export class Task {
    key: string;
    path: string;
    content: string;
    overridable: boolean;
    constructor(key, path, content, overridable) {
        this.key = key;
        this.path = path;
        this.content = content;
        this.overridable = overridable;
    }

    setPath(path) {
        this.path = path;
    }

    setContent(content) {
        this.content = content;
    }

    setOverridable(overridable) {
        this.overridable = overridable;
    }


    generate() {
        if (!this.content) return;
        let write = true;
        if (!fs.existsSync(this.path)) {
            file_.mkdir(file_.getDirName(this.path))
        } else {
            if (!this.overridable) {
                write = false;
            }
        }
        if (write) {
            fs.writeFileSync(this.path, this.content)
            console.info('generate %s, file: %s', this.key, this.path)
        }
    }

    execute() {
        this.generate()
    }
}